import SIdentify from "../../components/SIdentify.vue"
import request from "../../utils/request";
import {pcaTextArr} from "element-china-area-data";

export default {
    name: "login",
    components: {SIdentify},
    data() {
        return {
            formLogin: {
                name: "",
                password: "",
                code: ""
            },
            registerform: {
                image: "",
                name: "",
                actualname: "",
                password: "",
                phone: "",
                email: "",
                address: ""
            },
            image: '',
            labelPosition: 'left',
            registerdialogFormVisible: false,
            pcaTextArr,
            uploadFile: null,
            identifyCodes: '1234567890abcdefjhijklinopqrsduvwxyz',//随机串内容
            identifyCode: '',
            // 校验
            rules: {
                name: [{required: true, message: "请输入用户名", trigger: "blur"}],
                password: [{required: true, message: "请输入密码", trigger: "blur"}],
                code: [{required: true, message: "请输入验证码", trigger: "blur"}]
            },
            registerrules: {
                name: [{required: true, message: "请输入用户名", trigger: "blur"}],
                actualname: [{required: true, message: "请输入真实姓名", trigger: "blur"}],
                password: [{required: true, message: "请输入密码", trigger: "blur"}],
                phone: [{required: true, message: "请输入电话", trigger: "blur"}],
                email: [{required: true, message: "请输入邮箱", trigger: "blur"}],
                address: [{required: true, message: "请选择地址", trigger: "blur"}],
            }
        }
    },
    mounted() {
        // 初始化验证码
        this.identifyCode = ''
        this.makeCode(this.identifyCodes, 4)
    },
    methods: {
        imgSeccess(res, file) {
            console.log(res.data)
            this.registerform.image = res.data.toString();
        },
        //reset all information in the dialog of Register
        editDialogClosed() {
            this.$refs['registerform'].resetFields();
        },
        refreshCode() {
            this.identifyCode = ''
            this.makeCode(this.identifyCodes, 4)
        },
        makeCode(o, l) {
            for (let i = 0; i < l; i++) {
                this.identifyCode += this.identifyCodes[this.randomNum(0, this.identifyCodes.length)]
            }
        },
        randomNum(min, max) {
            return Math.floor(Math.random() * (max - min) + min)
        },
        loginByNameAndPwd() {
            this.$refs['formLogin'].validate((valid) => {
                if (valid) {
                    request.post('/login/nameandpwd', this.formLogin).then(res => {
                        if (this.formLogin.code.toLowerCase() == this.identifyCode.toLowerCase()) {
                            if (res.code === '200') {
                                if(res.data.userType == '0'){
                                    localStorage.setItem("name", JSON.stringify(res.data))
                                    this.$store.commit("setUser", res.data.user)
                                    let username = res.data.name
                                    let message = '欢迎您，' + username
                                    this.$message.success(message)
                                    this.$router.push("/countIndex")
                                } else if(res.data.userType == '1'){
                                    localStorage.setItem("name", JSON.stringify(res.data))
                                    this.$store.commit("setUser", res.data.user)
                                    let username = res.data.name
                                    let message = '欢迎您，' + username
                                    this.$message.success(message)
                                    this.$router.push("/index")
                                }else {

                                }
                            } else {
                                this.$message.error(res.msg)
                            }
                        } else {
                            this.$message.error("验证码错误，请重试!")
                            this.refreshCode()
                        }
                    })
                } else {
                    return false
                    location.reload()
                }
            })
        },
        toRegsiter() {
            this.registerdialogFormVisible = true
        },
        handleRegister() {
            this.$refs['registerform'].validate((valid) => {
                if (valid) {
                    this.registerform.address = this.registerform.address.join('')
                    request.post('/register/usersreg', this.registerform, {}).then(res => {
                        if (res.code === '200') {
                            this.$message({
                                type: "success",
                                message: "注册成功！"
                            })
                            this.registerdialogFormVisible = false
                            location.reload();
                        } else if (res.code === '-1') {
                            this.$message.error(res.msg)
                        } else if (res.code === '-101') {
                            this.$message.error(res.msg)
                        } else if (res.code === '-110') {
                            this.$message.error(res.msg)
                        } else if (res.code === '-100') {
                            this.$message.error(res.msg)
                        }
                    })
                }
            })
        }
    }
}